package com.leyou.controller;


import com.baomidou.mybatisplus.extension.api.R;
import com.leyou.dto.CategoryDTO;
import com.leyou.pojo.Category;
import com.leyou.pojo.CategoryBrand;
import com.leyou.service.CategoryBrandService;
import com.leyou.service.CategoryService;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import java.util.List;
import java.util.stream.Collectors;


/**
 * @author jshstart
 * @creat 2020-12-09 17:22
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private CategoryBrandService categoryBrandService;
    @GetMapping("{id}")
    public ResponseEntity<CategoryDTO> queryCategoryById(@PathVariable("id") Long id){
        Category category = categoryService.query().eq("id", id).one();
        CategoryDTO categoryDTO = new CategoryDTO(category);
        return ResponseEntity.ok(categoryDTO);
    }
    @GetMapping("/list")
    public ResponseEntity<List<CategoryDTO>> queryCategoryByIds(@RequestParam List<Long> ids){
        List<Category> categoryList = categoryService.listByIds(ids);
        List<CategoryDTO> dtoList = categoryList.stream().map(category -> new CategoryDTO(category)).collect(Collectors.toList());
        return ResponseEntity.ok(dtoList);
    }
    @GetMapping("/of/brand")
    public ResponseEntity<List<CategoryDTO>> queryCategoryByBrandId(@RequestParam Long id){
        List<CategoryBrand> categoryBrandList = categoryBrandService.query().eq("brand_id", id).list();
        for (CategoryBrand categoryBrand : categoryBrandList) {
            categoryBrand.getCategoryId();
        }
        List<Long> categoryIds = categoryBrandList.stream().map(categoryBrand -> categoryBrand.getCategoryId()).collect(Collectors.toList());
        List<Category> categoryList = categoryService.listByIds(categoryIds);
        List<CategoryDTO> dtoList = categoryList.stream().map(category -> new CategoryDTO(category)).collect(Collectors.toList());
        return ResponseEntity.ok(dtoList);
    }
    @GetMapping("/of/parent")
    public ResponseEntity<List<CategoryDTO>> queryCategoryByParentId(@RequestParam Long pid){
        List<Category> categoryList = categoryService.query().eq("parent_id", pid).list();
        List<CategoryDTO> dtoList = categoryList.stream().map(CategoryDTO::new).collect(Collectors.toList());
        return ResponseEntity.ok(dtoList);
    }
}
